package com.pulanit.pangu.admin.test.controller;


import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gitee.pulanos.pangu.framework.sdk.dto.Result;
import com.gitee.pulanos.pangu.framework.sdk.dto.page.PageResult;
import com.gitee.pulanos.pangu.framework.sdk.dto.page.PagingUtil;
import com.pulanit.pangu.admin.test.entity.Book;
import com.pulanit.pangu.admin.test.entity.BookPageIn;
import com.pulanit.pangu.admin.test.service.IBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 文稿 前端控制器
 * </p>
 *
 * @author 天鸣
 * @since 2024-02-20
 */
@RestController
@RequestMapping("/open/book")
public class BookController {

    @Autowired
    private IBookService bookService;

    /**
     * 列表
     */
    @GetMapping("/page")
    public Result<?> list(BookPageIn bookPageIn) {
        Page<Book> page = new Page<>(bookPageIn.getPageNo(), bookPageIn.getPageSize());
        LambdaQueryWrapper<Book> wrapper = new LambdaQueryWrapper<>();
        //条件
        wrapper.eq(ObjectUtil.isNotEmpty(bookPageIn.getStatus()), Book::getStatus, bookPageIn.getStatus())
                .like(StrUtil.isNotEmpty(bookPageIn.getName()), Book::getName, bookPageIn.getName());
        Page<Book> resultPage = bookService.page(page, wrapper);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("pageData", resultPage.getRecords());
        hashMap.put("total", resultPage.getTotal());

        return Result.success(hashMap);
    }

    /**
     * 获取列表
     *
     * @return
     */
    @GetMapping("/list")
    public Result<List<Book>> list() {
        List<Book> list = bookService.list();
        return Result.success(list);
    }

    /**
     * 获取详情
     *
     * @return
     */
    @GetMapping("/detail")
    public Result<Book> detail(@RequestParam("id") Long id) {
        Assert.notNull(id, "ID 不能为空");
        Book book = bookService.getById(id);
        return Result.success(book);
    }

    /**
     * 保存
     *
     * @param book
     * @return
     */
    @PostMapping("/add")
    public Result<Void> add(@RequestBody Book book) {
        bookService.save(book);
        return Result.success();
    }

    /**
     * 修改
     *
     * @param book
     * @return
     */
    @PatchMapping ("/update")
    public Result<Void> update(@RequestBody Book book) {
        bookService.updateById(book);
        return Result.success();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    public Result<Void> delete(@RequestParam("id") Long id) {
        Assert.notNull(id, "人员 ID 不能为空");
        bookService.removeById(id);
        return Result.success();
    }

    /**
     * 批量删除
     */
    @PostMapping("/batchDelete")
    public Result<Void> batchDelete(@RequestParam List<Long> ids) {
        Assert.notEmpty(ids, "人员 ID 不能为空");
        bookService.removeBatchByIds(ids);
        return Result.success();
    }

}
